package com.net.wanjian.networkhospitalmanager.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static android.content.ContentValues.TAG;
import static com.zhy.autolayout.utils.L.debug;

/**
 * Description:
 * Created by lidongcai on 17/5/10.
 * Email:  lidongcaiocc@163.com
 */

public class TimeDateUtils {
    private static SimpleDateFormat sFormat;

    public static final String SHORT_TIME_STR = "yyyy年MM月dd日";
    public static final int HOUR_SECOND = 3600; // 1小时3600秒

    public static String getTimeStr(Date date, String format) {
        sFormat = new SimpleDateFormat(format);
        return sFormat.format(date);
    }

    /**
     * @param millsecond
     * @return
     */
    public static String getTimeStrByMillSeconds(long millsecond) {

        Date date = new Date(millsecond);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = formatter.format(date);

        return format;
    }

    /**
     * 将秒数转换为小时/分钟/秒
     *
     * @param second
     * @return
     */
    public static String getTimeStr(int second) {
        // 如果秒数不足一小时,则返回的字符串格式为00:00,否则返回00:00:00
        int h, m, s;
        StringBuffer sb = new StringBuffer();
        if (second < HOUR_SECOND) {
            m = second / 60;
            s = second % 60;
            if (m < 10) {
                sb.append("0" + m);
            } else {
                sb.append(m);
            }
            sb.append(":");
            if (s < 10) {
                sb.append("0" + s);
            } else {
                sb.append(s);
            }
        } else {
            h = second / HOUR_SECOND;
            m = (second % HOUR_SECOND) / 60;
            s = (second % HOUR_SECOND) % 60;
            if (h < 10) {
                sb.append("0" + h);
            } else {
                sb.append(h);
            }
            sb.append(":");
            if (m < 10) {
                sb.append("0" + m);
            } else {
                sb.append(m);
            }
            sb.append(":");
            if (s < 10) {
                sb.append("0" + s);
            } else {
                sb.append(s);
            }
        }
        return sb.toString();
    }

    /**
     * 将毫秒转换为年月日时分秒
     * 输出格式为2015-05-29 20:35:00
     *
     * @author lidongcai
     */
    public static String getYearMonthDayHourMinuteSecond(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year = calendar.get(Calendar.YEAR);

        @SuppressLint("WrongConstant") int month = calendar.get(Calendar.MONTH) + 1;
        String mToMonth = null;
        if (String.valueOf(month).length() == 1) {
            mToMonth = "0" + month;
        } else {
            mToMonth = String.valueOf(month);
        }

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay = null;
        if (String.valueOf(day).length() == 1) {
            dToDay = "0" + day;
        } else {
            dToDay = String.valueOf(day);
        }

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour = null;
        if (String.valueOf(hour).length() == 1) {
            hToHour = "0" + hour;
        } else {
            hToHour = String.valueOf(hour);
        }

        int minute = calendar.get(Calendar.MINUTE);
        String mToMinute = null;
        if (String.valueOf(minute).length() == 1) {
            mToMinute = "0" + minute;
        } else {
            mToMinute = String.valueOf(minute);
        }

        int second = calendar.get(Calendar.SECOND);
        String sToSecond = null;
        if (String.valueOf(second).length() == 1) {
            sToSecond = "0" + second;
        } else {
            sToSecond = String.valueOf(second);
        }
        return year + "-" + mToMonth + "-" + dToDay + " " + hToHour + ":" + mToMinute + ":" + sToSecond;
    }

    /**
     * 将日期格式化为毫秒
     * str格式为2015-05-29 20:35:15
     *
     * @author lidongcai
     */
    public static long getMillion(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取当天的日期
     * str格式为2015-05-29 20:35:15
     *
     * @author lidongcai
     */
    public static String getCurrentDayDate(String str) {
        String date = str.split(" ")[0];
        String day = date.split("-")[2];

        return day;
    }

    /**
     * 获取当天的月份
     * str格式为2015-05-29 20:35:15
     *
     * @author lidongcai
     */
    public static String getCurrentMonth(String str) {
        String date = str.split(" ")[0];
        String month = date.split("-")[1];
        return month;
    }

    /**
     * 判断字符串是否已经到时间
     *
     * @param str
     * @return
     */
    public static boolean isStopTime(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Date date1 = new Date();
        date1.setTime(System.currentTimeMillis());
        if (date.before(date1)){
//            Log.e(TAG, "isStopTime:True "+date.getTime()+"毫秒"+date1.getTime());
            return true;
        }else {
//            Log.e(TAG, "isStopTime:False "+date.getTime()+"毫秒"+date1.getTime());
            return false;
        }

//        if (date.getTime() <= System.currentTimeMillis()) {
//            Log.e(TAG, "isStopTime:True "+date.getTime()+"毫秒"+System.currentTimeMillis());
//            return true;
//        } else {
//            Log.e(TAG, "isStopTime:False "+date.getTime()+"毫秒"+System.currentTimeMillis());
//            return false;
//        }
    }

    /**
     * 判断是否是开放那天，传递具体的时间，
     *
     * @param str
     * @return
     */
    public static boolean isOpenTime(String str) {
        String openTime = formatTime(str).split(" ")[0];
        Log.e(TAG, "isOpenTime: "+openTime );
        String currentTime = getYearMonthDay(System.currentTimeMillis()).split(" ")[0];
        Log.e(TAG, "currentTime: "+currentTime );
        if (openTime.equals(currentTime)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 2017/04/23 12:23:00 格式化为 2017－04－23 12:23:00
     *
     * @return
     */
    public static String formatTime(String str) {
        String replaceAll = str.replaceAll("/", "-");
        return replaceAll;
    }

    /**
     * 2017/4/9 2:3:00 格式化为 2017－04－09 02:03:00
     * @return
     */
    public static String formatTimeFriend(String str){

        String year = null,month = null,date = null,hour = null,min = null;

        String[] split = str.split(" ");//两段

        String[] dateStrings = split[0].split("/");
        String[] timeStrings = split[1].split(":");

        year = dateStrings[0];

        if (dateStrings[1].length() == 1){
            month = "0"+dateStrings[1];
        }else {
            month = dateStrings[1];
        }

        if (dateStrings[2].length() == 1){
            date = "0"+dateStrings[2];
        }else {
            date = dateStrings[2];
        }

        if (timeStrings[0].length() == 1){
            hour = "0"+timeStrings[0];
        }else {
            hour = timeStrings[0];
        }

        if (timeStrings[1].length() == 1){
            min = "0"+timeStrings[1];
        }else {
            min = timeStrings[1];
        }

        String currentDate = getYearMonthDayHourMinuteSecond(System.currentTimeMillis()).split(" ")[0];
        String d = year+"-"+month+"-"+date;
        if (currentDate.equals(d)){
            return "今天:"+" "+hour+":"+min;
        }

        return d+" "+hour+":"+min;
    }

    /**
     * 将毫秒转换为年月日时分秒
     * 输出格式为2015-5-29 20:35:00 不带0版本
     *
     * @author lidongcai
     */
    public static String getYearMonthDay(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year = calendar.get(Calendar.YEAR);

        @SuppressLint("WrongConstant") int month = calendar.get(Calendar.MONTH) + 1;
        String mToMonth = null;

            mToMonth = String.valueOf(month);


        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay = null;

            dToDay = String.valueOf(day);


        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour = null;

            hToHour = String.valueOf(hour);


        int minute = calendar.get(Calendar.MINUTE);
        String mToMinute = null;

            mToMinute = String.valueOf(minute);


        int second = calendar.get(Calendar.SECOND);
        String sToSecond = null;

            sToSecond = String.valueOf(second);

        return year + "-" + mToMonth + "-" + dToDay + " " + hToHour + ":" + mToMinute + ":" + sToSecond;
    }
}
